import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/normal_entity.dart';
import 'package:wanxiang_app/bean/qa_list_entity.dart';
import 'package:wanxiang_app/pages/home/qaDetail.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../bean/service_list_entity.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';

import '../login/agreement.dart';

class ServiceController extends GetxController{

  @override
  void onInit() {
    super.onInit();
    type = Get.arguments;
    if(type=='帮助中心'){
      _getHelp();
    }else{
      _getService();
    }

  }

  String type = '常见问题';

  @override
  void onClose() {
    super.onClose();
  }

  List<QaListData> list = [];
  void _getService(){
    DioManager.getInstance().post(ServiceUrl.qaList,null, (data) async {
      QaListEntity bannerEntity = QaListEntity.fromJson(jsonDecode(data));
      list = bannerEntity.data;
      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }
  void _getHelp(){
    DioManager.getInstance().post(ServiceUrl.helpList,null, (data) async {
      QaListEntity bannerEntity = QaListEntity.fromJson(jsonDecode(data));
      if(type=='帮助中心'){
        QaListData qaListData = QaListData();
        qaListData.id = -1;
        qaListData.title = '用户协议';
        qaListData.content = '';
        qaListData.sortId=-1;
        qaListData.platform ='app';
        list.add(qaListData);

        QaListData qaListData2 = QaListData();
        qaListData2.id = -1;
        qaListData2.title = '隐私协议';
        qaListData2.content = '';
        qaListData2.sortId=-1;
        qaListData2.platform ='app';
        list.add(qaListData2);

      }
      list.addAll(bannerEntity.data);
      // list = bannerEntity.data;
      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }
}


class QAPage extends GetView<ServiceController>  {
  late ServiceController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<ServiceController>(() => ServiceController());
    controller = Get.find();
    context = context1;

    return GetBuilder<ServiceController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: controller.type,hasLeading: true,),
            backgroundColor: Colors.white,
            body:_content());
      },
    )
    ;

  }
  Widget _content(){
    return SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(left: 20.w,top: 20.h,right: 20.w,bottom: 20.h),
          child: Column(
            children: _all(),
          ),
        )
    ) ;
  }


  List<Widget> _all(){
    List<Widget> aa = [];
    controller.list.forEach((element) {
      aa.add(_item(element));
    });
    return aa;
  }

  Widget _item(item){
    return InkWell(
      onTap: (){
        if(item.title=='用户协议'){
          Get.to(()=>AgreementPage(),arguments: '用户协议');
        }else  if(item.title=='隐私协议'){
          Get.to(()=>AgreementPage(),arguments: '隐私协议');
        }else{
          Get.to(()=>QADetailPage(),arguments: item,preventDuplicates:false);
        }

      },
      child: Container(
        height: 71.h,
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(width: 1.h,color: Color(0xFFF5F5F5))
            )
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(item.title),
            Image.asset(Constant.IMG_BASE+'arrow_right.png',width: 6.w,height: 11.h,)
          ],
        ),
      )
    ) ;
  }

}